package io.github.eric.urpc.example.example;

import io.github.eric.urpc.client.Client;
import io.github.eric.urpc.client.ClientConfig;
import io.github.eric.urpc.core.util.RandomStringGenerator;
import io.github.eric.urpc.example.msg.BigMessage;
import io.github.eric.urpc.server.Server;
import io.github.eric.urpc.server.ServerConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.TimeUnit;

public class SendBigMessageWithoutRegExample {
    private static final Logger LOGGER = LoggerFactory.getLogger(SendBigMessageWithoutRegExample.class);

    public static void main(String[] args) {
        // init config
        ServerConfig serverConfig = new ServerConfig.Builder()
//                .nettyLoggingLevel(LogLevel.INFO)
                .applicationName("simpleServer")
                .listenPort(52379)
                .frameMaxLength(100*1024*1024)
                .build();
        // init server
        Server server = new Server(serverConfig);
        server.start();

        // init config
        ClientConfig clientConfig = new ClientConfig.Builder()
//                .nettyLoggingLevel(LogLevel.INFO)
                .applicationName("simpleClient")
                .serverHost("127.0.0.1")
                .serverPort(52379)
                .frameMaxLength(100*1024*1024)
                .build();
        // init client and connect to server
        Client client = new Client(clientConfig);


        BigMessage bigMessage = new BigMessage();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 1024*1024*10; i++) {
            sb.append(RandomStringGenerator.genRandomString(1));
        }
        bigMessage.setStr(sb.toString());

        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    try {
                        Thread.sleep(3000L);

                        client.sendAsync(bigMessage);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();

        try {
            Thread.sleep(TimeUnit.DAYS.toMillis(1));
        } catch (Exception ignore) {}

        client.shutdown();
        server.shutdown();
    }
}
